package com.example.seckill.exception;

import com.example.seckill.common.CodeMsg;
import com.example.seckill.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SeckillException.class)
    public Result<String> handleSeckillException(SeckillException e) {
        log.error("业务异常", e);
        return Result.error(e.getCodeMsg());
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public Result<String> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
        log.error("参数类型错误", e);
        return Result.error(CodeMsg.PARAM_ERROR);
    }

    @ExceptionHandler(DataAccessException.class)
    public Result<String> handleDataAccessException(DataAccessException e) {
        log.error("数据库访问异常", e);
        return Result.error(CodeMsg.DB_ERROR);
    }

    @ExceptionHandler(AmqpException.class)
    public Result<String> handleAmqpException(AmqpException e) {
        log.error("消息队列异常", e);
        return Result.error(CodeMsg.MQ_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public Result<String> handleException(Exception e) {
        log.error("系统异常", e);
        return Result.error(CodeMsg.SERVER_ERROR);
    }
} 